# import time
from . import ConnPG
import psycopg2
from sanic.log import logger

class StorageLocationTable:

    async def add_storage_location(self, location_name, warehouse_id):#添加库房

        #参数检查

        if not all([location_name, warehouse_id]) or not warehouse_id.isdecimal():

            return {
                'status': -1,
                'errorCode': 301100,
                'errorMsg': '请检查参数，有两个必参数且warehouse_id为整数',
                'resultBody': None
            }

        sql = 'INSERT INTO storage_location(location_name, warehouse_id) VALUES (%s, %s)'

        try:
            await ConnPG().execute(sql, (location_name, warehouse_id))
        except psycopg2.errors.UniqueViolation:

            return {
                'status': -1,
                'errorCode': 301101,
                'errorMsg': '当前库位名已经存在',
                'resultBody': None,
            }
        else:
        
            return {
                'status': 1,
                'errorCode': None,
                'errorMsg': None,
                'resultBody': "库位添加成功",
            }

    async def select_warehouse_location(self, warehouse_id):#查看指定库房的库位

         #参数检查

        if not all([warehouse_id]) or not warehouse_id.isdecimal():

            return {
                'status': -1,
                'errorCode': 301102,
                'errorMsg': '请检查参数，有一个必参数且warehouse_id为整数',
                'resultBody': None
            }

        sql = 'select * from storage_location where warehouse_id=%s'

        result = await ConnPG().fetchall(sql, (warehouse_id,))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': result,
        }

    